package com.ruoyi.app.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.app.domain.realtimepositioning.DeviceMileageParam;
import com.ruoyi.app.domain.realtimepositioning.QueryDeviceMileageParam;
import com.ruoyi.app.domain.realtimepositioning.QueryVehicleListParam;
import com.ruoyi.app.domain.realtimepositioning.VehicleVideoParam;
import com.ruoyi.app.service.AppRealtimePositioningService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.JSONObjectPro;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.BigDataResponse;
import com.ruoyi.common.utils.http.HttpRequest;
import com.ruoyi.system.domain.vo.common.DeviceGps;
import com.ruoyi.system.service.IEquipmentManagementService;
import com.ruoyi.system.service.IVehicleEquipmentService;
import com.ruoyi.system.utils.ComLbsUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StopWatch;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 描述 APP实时定位
 * 作者 ZhangPing
 * 创建时间 2023/9/28 14:26
 */
@Slf4j
@RestController
@RequestMapping("/app/realtimePositioning")
public class AppRealtimePositioningController extends BaseController {

    @Autowired
    private AppRealtimePositioningService realtimePositioningService;
    @Autowired
    private IEquipmentManagementService equipmentManagementService;

    @ApiOperation("获取地图车辆集合")
    @PostMapping("/queryGpsList")
    public AjaxResult queryGpsList(@RequestParam(value = "orderStatus", required = false ) Long orderStatus,
                                   @RequestParam(value = "companyId", required = false ) Long companyId) {
        LoginUser loginUser = getLoginUser();
        if (companyId == null){
            companyId = loginUser.getDeptId();
        }
        return success(realtimePositioningService.queryGpsList(orderStatus, companyId));
    }


    @ApiOperation("获取车辆实时定位信息")
    @PostMapping("/queryGpsInfo")
    public AjaxResult queryGpsInfo(@RequestParam String dvo) {
        return success(realtimePositioningService.queryGpsInfo(dvo));
    }

    @ApiOperation("按名称搜索车辆")
    @PostMapping("/searchVehicle")
    public AjaxResult searchVehicle(@RequestParam String plateNumber, @RequestParam (value = "companyIds", required = false ) List<Long> companyIds) {
        return success(equipmentManagementService.listNumberPlateDeviceIdByName(new JSONObjectPro()
                .put("plateNumber", plateNumber)
                .put("companyIds", companyIds)));
    }

    @ApiOperation("车辆里程统计排行")
    @PostMapping("/queryDeviceMileageRank")
    public AjaxResult queryDeviceMileageRank() {
        return success(realtimePositioningService.queryDeviceMileageRank());
    }

    @ApiOperation("数据权限树")
    @PostMapping("/dataTree")
    public AjaxResult dataTree() {
        LoginUser loginUser = getLoginUser();
        return success(realtimePositioningService.dataTree(loginUser.getDeptId()));
    }

    @ApiOperation("车辆列表")
    @PostMapping("/queryVehicleList")
    public AjaxResult queryVehicleList(@RequestBody QueryVehicleListParam param) {
        return success(realtimePositioningService.queryVehicleList(param));
    }

    @ApiOperation("车辆里程统计")
    @PostMapping("/queryDeviceMileage")
    public AjaxResult queryDeviceMileage(@Validated QueryDeviceMileageParam param) {
        return success(realtimePositioningService.queryDeviceMileage(param));
    }

    @ApiOperation(value = "车辆历史轨迹")
    @PostMapping(value = "/getVehicleHisSegmentTracksAPP")
    public AjaxResult vehicleHisSegmentTracks(@Validated QueryDeviceMileageParam param) {
        return success(realtimePositioningService.vehicleHisSegmentTracks(param));
    }

    @ApiOperation(value = "车辆历史轨迹详情")
    @PostMapping(value = "/getVehicleHisTracksDetail")
    public AjaxResult vehicleHisTracksDetail(@Validated QueryDeviceMileageParam param) {
        return success(realtimePositioningService.vehicleHisTracksDetail(param));
    }

    @ApiOperation("车辆实时视频一键点播")
    @PostMapping("/getVehicleVideos")
    public AjaxResult vehicleVideos(@Validated VehicleVideoParam param) {
        return success(realtimePositioningService.vehicleVideos(param));
    }

    @ApiOperation("车辆里程统计列表")
    @PostMapping("/queryDeviceMileagePage")
    public AjaxResult queryDeviceMileagePage(@Validated @RequestBody DeviceMileageParam param) {
        return success(realtimePositioningService.queryDeviceMileagePage(param));
    }

    @ApiOperation("有权限的监管类型列表")
    @GetMapping("/getObjTypesUser")
    public AjaxResult getObjTypesUser() {
        return success(equipmentManagementService.listObjType(new JSONObject()));
    }

}
